package cn.edu.heuet.bnve.remind;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.WindowManager;


import androidx.annotation.NonNull;

import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import cn.edu.heuet.bnve.R;
import cn.edu.heuet.bnve.model.Schedule;
import cn.edu.heuet.bnve.utils.RemindUtil;
import cn.edu.heuet.bnve.utils.SharepreferencesUtilSystemSettings;

/**
 * Created by abc-pc on 2020/4/21.
 */
public class AlarmAlertBroadcastReceiver extends BroadcastReceiver {

    private MediaPlayer mediaPlayer;
    private Vibrator vibrator;
    private boolean isVibrator = false;
    private Context context;
    private boolean onReceiived=false;


    @Override
    public void onReceive(Context context, Intent intent) {
        this.context=context;
        Log.i("wqs","111");
        if(!onReceiived){
            Log.i("wqs","通知来了");
            onReceiived=true;
        }
        try {
            String title  = intent.getStringExtra("scheduleTitle");
            int type = intent.getIntExtra("alarmType",0);
            SharepreferencesUtilSystemSettings.putValue(context,"scheduleTitle",title);
            SharepreferencesUtilSystemSettings.putValue(context,"alarmType",type);
        }
        catch (Exception e){
            Log.i("wqs",e.toString());
        }

        if(SharepreferencesUtilSystemSettings.getValue(context,"schedule_notify1",false)) {
            if (SharepreferencesUtilSystemSettings.getValue(context, "schedule_notify_model", 0) == 0) {
                RemindUtil.sendNotification(context);
                playMusicAndVibrate(context);
            } else {
                showAlarmDialog(context);
                playMusicAndVibrate(context);
            }
        }

        Intent alarmServiceIntent = new Intent(
                "AlarmServiceBroadcastreceiver");
        context.sendBroadcast(alarmServiceIntent, null);

    }

    private void showAlarmDialog(Context context) {


        new MaterialDialog.Builder(context)
                .iconRes(R.drawable.ic_checked_right)
                .title("您定的日期已经到了哦！")
                .content("日程："+SharepreferencesUtilSystemSettings.getValue(context,"scheduleTitle","通知"))
                .positiveText("OK")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        if (mediaPlayer != null) {
                            mediaPlayer.stop();
                            mediaPlayer.release();
                            mediaPlayer = null;
                        }
                    }
                })
                .show();
    }
    /**
     * 播放音乐
     * @param context
     */
    private void playMusicAndVibrate(Context context){
        String path;
        path = SharepreferencesUtilSystemSettings.getValue(context,"schedule_notify_soundPath",null);
        if(path!=null) {
            Uri ringtoneUri = Uri.parse(path);
            if(ringtoneUri!=null) {
                if (SharepreferencesUtilSystemSettings.getValue(context, "schedule_notify_isSound1", false)) {
                    if (mediaPlayer == null) {
                        mediaPlayer = new MediaPlayer();
                    } else {
                        if (mediaPlayer.isPlaying())
                            mediaPlayer.stop();
                        mediaPlayer.reset();
                    }
                    try {
                        mediaPlayer.setVolume(100f, 100f);
                        mediaPlayer.setDataSource(context, ringtoneUri);
                        mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                        mediaPlayer.setLooping(false);
                        mediaPlayer.prepare();
                        mediaPlayer.start();

                        //播放时间
                        Timer timer = new Timer();
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                //doSomething
                                mediaPlayer.stop();
                            }
                        }, 4000);


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        if (SharepreferencesUtilSystemSettings.getValue(context,"schedule_notify_isVibrate1",false)) {
            vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(500);
        }
    }
}
